package com.buu.manager.controller;

import com.buu.common.annotation.Log;
import com.buu.common.utils.MD5Utils;
import com.buu.common.utils.PageUtils;
import com.buu.common.utils.Query;
import com.buu.common.utils.R;
import com.buu.manager.domain.EduDO;
import com.buu.manager.service.EduService;
import com.buu.manager.service.GradeService;
import com.buu.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教师信息维护
 */
@RequestMapping("/manager/teacher")
@Controller
public class TeacherCtroller {

    @Autowired
    private EduService eduService;

    @Autowired
    private UserService userService;

    @Autowired
    private GradeService gradeService;

    @GetMapping("")
    String teacher(Model model) {
        return "manager/teacher/teacher";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<EduDO> eduList = eduService.list(query);
        int total = eduService.count(query);
        PageUtils pageUtil = new PageUtils(eduList, total);
        return pageUtil;
    }

    @Log("添加用户信息")
    @GetMapping("/add")
    String add(Model model) {
        return "manager/teacher/add";
    }

    @Log("编辑用户信息")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        EduDO eduDO = eduService.get(id);
        model.addAttribute("user", eduDO);
        List grades = gradeService.list();
        model.addAttribute("grades", grades);
        return "manager/teacher/edit";
    }

    @Log("更新用户信息")
    @PostMapping("/update")
    @ResponseBody
    R update(EduDO eduDo) {
        if(eduService.update(eduDo) > 0){
            return R.ok();
        }
        return R.error();
    }


    @Log("保存用户信息")
    @ResponseBody
    @PostMapping("/save")
    R save(EduDO eduDo) {

        if (eduService.save(eduDo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Log("重置用户密码")
    @PostMapping("/resetPwd")
    @ResponseBody
    R resetPwd(@RequestParam("id") String id) {
        EduDO eduDO = eduService.get(Long.valueOf(id));
        String passwd = MD5Utils.encrypt(eduDO.getUserName(), "123456");

        if(userService.resetPasswd(passwd, eduDO.getSysUserId()) > 0){
            return R.ok();
        }
        return R.error();
    }


    @Log("删除用户")
    @PostMapping("/remove")
    @ResponseBody
    Map<String, Boolean> remove(Long id) {
        Map<String, Boolean> rMap = new HashMap<>();
        rMap.put("success", eduService.remove(id) > 0);
        return rMap;
    }

    @Log("批量删除用户")
    @PostMapping("/batchRemove")
    @ResponseBody
    R batchRemove(@RequestParam("ids[]") Long[] userIds) {

        List<Long> Ids = Arrays.asList(userIds);
        int r = eduService.batchRemove(Ids);
        if (r > 0) {
            return R.ok();
        }
        ;
        return R.error();
    }


}
